App.controller('discountController', ['$rootScope', '$scope', '$state', 'Page', 'XCode',function($rootScope, $scope, $state, Page, Xode) {

    $scope.discount = {};
    //$scope.discountDetail = {};

    function isRepeat(discountDetails){
        var hash = {};
        for(var i in discountDetails) {
            if(hash[discountDetails[i].days])
                return true;
            hash[discountDetails[i].days] = true;
        }
        return false;
    }

    $scope.grid = {
        add: function () {
            //$scope.deleteDetailList = [];
            //记录时间段类型的起止时间
            $scope.beginDateArr = [];
            $scope.endDateArr = [];
            $scope.type = 1;
            $scope.discount = {discountDetails: []};
            $scope.discount.deleteDetailList = [];
            $scope.modal.show();
        },
        addDetail: function() {
            $scope.discount.discountDetails.push({});
        },
        deleteDetail: function(index) {
            //不进行数据库删除，记录需要删除的明细
            var obj = $scope.discount.discountDetails[index];
            if(obj.discountDetailId != undefined && obj.discountDetailId != "") { //对于已有的折扣方案
                $scope.discount.deleteDetailList.push(obj);
                $scope.discount.discountDetails.splice(index, 1);
                //Page.ajaxPost('discount/deleteDetail.do',{discountDetailId : obj.discountDetailId}, function() {
                //    $scope.discount.discountDetails.splice(index, 1);
                //});
            } else { //新增的折扣方案
                $scope.discount.discountDetails.splice(index, 1);
            }
        },
        save: function() {
            for (var i = 0; i < $scope.discount.discountDetails.length; i++) {
                var obj = $scope.discount.discountDetails[i];
                if($scope.discount.discountType == '1') { //累计天数
                    if(obj.days == undefined || obj.days == ''
                        || obj.discountRate == undefined || obj.discountRate == ''
                        || obj.discountDetailStatus == undefined || obj.discountDetailStatus == '') {
                        Page.notice("请填写必填项!");
                        return;
                    } else {
                        var x = new RegExp("^[0-9]*[1-9][0-9]*$");
                        if(!x.test(obj.days)) {
                            Page.notice("请填写正确的累计天数!");
                            return;
                        }
                    }
                } else { //时间段
                    if(obj.startDate == undefined || obj.startDate == ''
                        || obj.endDate == undefined || obj.endDate == ''
                        || obj.discountRate == undefined || obj.discountRate == ''
                        || obj.discountDetailStatus == undefined || obj.discountDetailStatus == '') {
                        Page.notice("请填写必填项!");
                        return;
                    } else {
                        if(moment(obj.endDate) < moment(obj.startDate)){
                            Page.notice("截止不能大于起始时间!");
                            return;
                        }
                    }
                    $scope.beginDateArr[i] = obj.startDate;
                    $scope.endDateArr[i] = obj.endDate;
                }
                if(!(obj.discountRate > 0 && obj.discountRate < 1)) {
                    Page.notice("请填写正确的折扣率(0-1之间)");
                    return;
                }

            }

            if($scope.discount.discountType == '1') {
                if(isRepeat($scope.discount.discountDetails)) {
                    Page.notice("累计天数重复!");
                    return;
                }
            } else {
                $scope.beginDateArr = $scope.beginDateArr.sort();
                $scope.endDateArr  = $scope.endDateArr.sort();
                for(j = 1; j < $scope.beginDateArr.length; j++){
                    if ($scope.beginDateArr[j] <= $scope.endDateArr[j-1]){
                        Page.notice("方案时间有重复!");
                        return;
                    }
                }

            }

            Page.ajaxPost($scope.discount.discountId ? 'discount/update.do' : 'discount/add.do', $scope.discount, function() {
                $scope.grid.query();
                $scope.grid.cancel();
            });
        },
        column_defs: [{
            target: 5,
            buttons: [
                {
                    name: '查看',
                    class: 'btn-success btm-sm',
                    icon: 'fa-search',
                    event: function(row) {
                        $scope.discount = row;
                        $scope.type = 3;
                        $scope.modal.show();
                    }
                },
                {
                    name: '编辑',
                    class: 'btn-warning btm-sm',
                    icon: 'fa-edit',
                    event: function(row) {
                        $scope.discount = row;
                        $scope.discount.deleteDetailList = [];
                        $scope.type = 2;
                        //记录时间段类型的起止时间
                        $scope.beginDateArr = [];
                        $scope.endDateArr = [];
                        $scope.modal.show();
                    }
                },
                {
                    name: '删除',
                    class: 'btn-danger',
                    icon: 'fa-close',
                    event: function(row) {
                        Page.confirm("确定要删除吗？", function() {
                            Page.ajaxPost('discount/delete.do', row, function() {
                                $scope.grid.query();
                            });
                        });
                    }
                }
            ]
        }]
    };

}]);